package com.volunteer.controller;

import com.volunteer.service.ProvincialCityService;
import com.volunteer.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author willow
 * @date 2019年03月13日
 */
@RestController
@RequestMapping("/provincial")  // 通过这里配置使下面的映射都在/users下
public class ProvincialCityController {
    /**
     * 抓取京东数据
     */
    private final static String GET_CATCHJDDATA = "/catchJDData";
    /**
     * 获取下一级地区数据
     */
    private final static String GET_PROVINCIAL = "/getProvincial";

    @Autowired
    private ProvincialCityService provincialCityService;

    @ApiOperation(value = "抓取京东数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "级别", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNumber", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数", dataType = "Integer")
    })
    @GetMapping(value = GET_CATCHJDDATA)
    public Result userInfo(@RequestParam("level") Integer level,
                           @RequestParam("pageNumber") Integer pageNumber,
                           @RequestParam(name = "pageSize", required = false) Integer pageSize) {
        pageSize = null == pageSize ? 20 : pageSize;
        return provincialCityService.catchJDData(level, pageNumber, pageSize);
    }

    @ApiOperation(value = "获取下一级地区数据")
    @ApiImplicitParam(name = "id", value = "父级id,默认中国", dataType = "Long")
    @GetMapping(value = GET_PROVINCIAL)
    public Result getProvincial(@RequestParam(name = "id", required = false) Long id) {
        id = null == id ? 4744L : id;
        return provincialCityService.getProvincial(id);
    }
}
